<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="模式名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'modeName', validatorRules.modeName]" placeholder="请输入模式名称"></a-input>
        </a-form-item>
        <a-form-item label="放大倍数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'magnificationMultiple', validatorRules.magnificationMultiple]"
                   placeholder="请输入放大倍数"></a-input>
        </a-form-item>
        <a-form-item label="贫困户户数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'poorhouseholdsNumber', validatorRules.poorhouseholdsNumber]"
                   placeholder="请输入贫困户户数"></a-input>
        </a-form-item>

        <a-form-item label="贫困户金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-tooltip>
            <template slot="title">
              单位:万元
            </template>
            <a-input v-decorator="[ 'poorhouseholdsMoney', validatorRules.poorhouseholdsMoney]"
                     placeholder="请输入贫困户金额"></a-input>
          </a-tooltip>

        </a-form-item>
        <a-form-item label="企业数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'companyNumber', validatorRules.companyNumber]" placeholder="请输入企业数"></a-input>
        </a-form-item>

        <a-form-item label="企业金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-tooltip>
            <template slot="title">
              单位:万元
            </template>
            <a-input v-decorator="[ 'companyMoney', validatorRules.companyMoney]" placeholder="请输入企业金额"></a-input>
          </a-tooltip>
        </a-form-item>
        <!-- <a-form-item label="合作单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
           <a-input v-decorator="[ 'partner', validatorRules.partner]" placeholder="请输入合作单位"></a-input>
         </a-form-item>
         <a-form-item label="金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
           <a-input v-decorator="[ 'money', validatorRules.money]" placeholder="请输入金额"></a-input>
         </a-form-item>
         <a-form-item label="使用时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
           <j-date placeholder="请选择使用时间" v-decorator="[ 'useDate', validatorRules.useDate]" :trigger-change="true" style="width: 100%"/>
         </a-form-item>
         <a-form-item label="支持产业" :labelCol="labelCol" :wrapperCol="wrapperCol">
           <a-textarea v-decorator="['supportIndustry']" rows="4" placeholder="请输入支持产业"/>
         </a-form-item>-->


      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

    import {httpAction} from '@/api/manage'
    import pick from 'lodash.pick'
    import JDate from '@/components/jeecg/JDate'

    export default {
        name: "UseOfFundsModal",
        components: {
            JDate,
        },
        data() {
            return {
                form: this.$form.createForm(this),
                title: "操作",
                width: 800,
                visible: false,
                model: {},
                labelCol: {
                    xs: {span: 24},
                    sm: {span: 5},
                },
                wrapperCol: {
                    xs: {span: 24},
                    sm: {span: 16},
                },

                confirmLoading: false,
                validatorRules: {
                    modeName:{rules: [{ required: true, message: '模式名称不能为空'}]},
                    partner: {},
                    money: {rules: [{ required: true, message: '到账金额不能为空'}]},
                    useDate: {},
                    magnificationMultiple: {rules: [{ required: true, message: '放大倍数不能为空'}]},
                    supportIndustry: {},
                    companyNumber:{rules: [{ required: true, message: '企业数不能为空'}]},
                    companyMoney:{rules: [{ required: true, message: '企业金额不能为空'}]},
                    poorhouseholdsMoney:{rules: [{ required: true, message: '贫困户金额不能为空'}]},
                    poorhouseholdsNumber:{rules: [{ required: true, message: '贫困户数不能为空'}]},
                },
                url: {
                    add: "/riskpreventionandcontrol/useOfFunds/add",
                    edit: "/riskpreventionandcontrol/useOfFunds/edit",
                }

            }
        },
        created() {
        },
        methods: {
            add() {
                this.edit({});
            },
            edit(record) {
                this.form.resetFields();
                this.model = Object.assign({}, record);
                this.visible = true;
                this.$nextTick(() => {
                    this.form.setFieldsValue(pick(this.model, 'partner', 'money', 'useDate', 'magnificationMultiple', 'supportIndustry', 'modeName', 'poorhouseholdsMoney', 'poorhouseholdsNumber', 'companyMoney', 'companyMoney'))
                })
            },
            close() {
                this.$emit('close');
                this.visible = false;
            },
            handleOk() {
                const that = this;
                // 触发表单验证
                this.form.validateFields((err, values) => {
                    if (!err) {
                        that.confirmLoading = true;
                        let httpurl = '';
                        let method = '';
                        if (!this.model.id) {
                            httpurl += this.url.add;
                            method = 'post';
                        } else {
                            httpurl += this.url.edit;
                            method = 'put';
                        }
                        let formData = Object.assign(this.model, values);
                        console.log("表单提交数据", formData)
                        httpAction(httpurl, formData, method).then((res) => {
                            if (res.success) {
                                that.$message.success(res.message);
                                that.$emit('ok');
                            } else {
                                that.$message.warning(res.message);
                            }
                        }).finally(() => {
                            that.confirmLoading = false;
                            that.close();
                        })
                    }

                })
            },
            handleCancel() {
                this.close()
            },
            popupCallback(row) {
                this.form.setFieldsValue(pick(row, 'partner', 'money', 'useDate', 'magnificationMultiple', 'supportIndustry', 'modeName', 'poorhouseholdsMoney', 'poorhouseholdsNumber', 'companyMoney', 'companyMoney'))
            }

        }
    }
</script>